package com.nari.ies.server.service.impl;

import com.nari.ies.server.mapper.CurveMapper;
import com.nari.ies.server.service.ICurveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class CurveServiceImpl implements ICurveService {

    @Autowired
    private CurveMapper curveMapper;

    @Override
    public List<Object> getCDQYCData(String plantId, Date startDate, Date endDate) {
        List<Object> ultraData = curveMapper.ultraData(plantId,startDate,endDate);
        return ultraData;
    }

    @Override
    public Object[] getYcHsCueveData(String yc_id, Date startDate, Date endDate, int step) {
        String strTableName = null;
        String strTableColumn = null;
        log.info("yc_id-------------" + yc_id);

        List<Map<String, Object>> mList = curveMapper.queryHistoryName(yc_id);
        List<Object[]> result = new ArrayList<>();
        for (Map<String, Object> map : mList) {
            Object[] mObj = new Object[3];
            mObj[0] = map.get("YC_ID");
            mObj[1] = map.get("HISTORY_TABLE_NAME");
            mObj[2] = map.get("HISTORY_COLUMN_NAME");
            result.add(mObj);
        }
        if (result.size() != 1) {
            log.info("sql执行无数据");
            log.info("查询采样定义表返回记录数：" + result.size() + "!=1,直接返回null");
            log.info("-------------------------");
            return null;
        }
        Object[] objs = result.get(0);
        // 表名
        strTableName = objs[1] == null ? null : objs[1].toString();
        log.info("表名"+ strTableName);
        // 域名
        strTableColumn = objs[2] == null ? null : objs[2].toString();
        log.info("域名"+ strTableColumn);
        List<Object[]> data = this.getHisData(strTableName, strTableColumn, startDate, endDate);
        // 计算数组大小
        int num = (int) ((endDate.getTime() - startDate.getTime()) / (step * 60 * 1000));
        log.info("数组大小:"+num);

        Object[] resultData = new Object[num];
        for (int i = 0; i < data.size(); i++) {
            //获取查询结果内的时间值
            Date occurTime = (Date) data.get(i)[0];
            Object value = data.get(i)[1];

            //与初始时间间隔时常-秒级别
            long tempL = (occurTime.getTime() - startDate.getTime()) / (60 * 1000);

            //时间间隔不满足步长要求-直接继续
            if ((tempL % step) != 0) {
                continue;
            }
            int nPos = (int) (tempL / step);
            if (value != null) {
                if (Float.parseFloat(String.valueOf(value)) < 0) {
                    value = 0.0;
                } else {
                    value = (float) (Math.round(Float.parseFloat(value.toString()) * 100)) / 100;
                }
            }
            resultData[nPos] = value;
        }

        log.info("总有功结果集:"+ Arrays.toString(resultData));
        return resultData;
    }

    @Override
    public List<LinkedHashMap<String, Object>> getDQYCData(String plantId, Date startDate) {
        List<LinkedHashMap<String, Object>> dqycData = curveMapper.getDQYCData(plantId,startDate);
        log.info("查询结果为："+dqycData);
        return dqycData;
    }

    @Override
    public List<Map<String, Object>> getDataCDQYC(String plantId, Date startDate, Date endDate) {
        List<Map<String, Object>> cdqycData = curveMapper.getDataCDQYC(plantId,startDate,endDate);
        return cdqycData;
    }

    @Override
    public Object getDQYCCurrent15Data(String column,String plantId, Date today) {
        Object dqycCurrent15Data = curveMapper.getDQYCCurrent15Data(column, plantId, today);
        return dqycCurrent15Data;
    }

    @Override
    public Object getDQYCCurrent5Data(String columnStart, String columnEnd, String column, String plantId, Date today,Integer num) {
        Object dqycCurrent15Data=curveMapper.getDQYCCurrent5Data(columnStart,columnEnd,column,plantId,today,num);
        return dqycCurrent15Data;
    }

    @Override
    public List<LinkedHashMap<String,Object>> getDQPoint5Data(String plantId, Date startDate) {
        List<LinkedHashMap<String,Object>> DQPoint5Data = curveMapper.getDQPoint5Data(plantId,startDate);
        log.info("短期预测查询结果为："+DQPoint5Data);
        return DQPoint5Data;
    }

    @Override
    public List<LinkedHashMap<String,Object>> getCDQPoint5Data(String plantId,Date endDate) {
        List<LinkedHashMap<String,Object>> CDQPoint5Data = curveMapper.getCDQPoint5Data(plantId,endDate);
        log.info("超短期预测查询结果为："+CDQPoint5Data);
        return CDQPoint5Data;
    }


    /**
     * 查询历史采样数据
     *
     * @param tableName
     * @param tableColumn
     * @param startDate
     * @param endDate
     * @return
     */
    private List<Object[]> getHisData(String tableName, String tableColumn, Date startDate, Date endDate) {
        Map<String, Object> params = new HashMap<>();
        params.put("tableName", tableName);
        params.put("tableColumn", tableColumn);
        params.put("startDate", startDate);
        params.put("endDate", endDate);

        List<Map<String, Object>> data = curveMapper.getHisData(params);
        List<Object[]> result = new ArrayList<>();
        for (Map<String, Object> map : data) {
            Object[] obj = new Object[2];
            obj[0] = map.get("TIME");
            obj[1] = map.get("VALUE");
            result.add(obj);
        }
        return result;
    }
}
